{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Maximum Difference Between Increasing Elements"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: maximumDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #增量元素之间的最大差值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的整数数组 <code>nums</code> ，该数组的大小为 <code>n</code> ，请你计算 <code>nums[j] - nums[i]</code> 能求得的 <strong>最大差值 </strong>，其中 <code>0 &lt;= i &lt; j &lt; n</code> 且 <code>nums[i] &lt; nums[j]</code> 。</p>\n",
    "\n",
    "<p>返回 <strong>最大差值</strong> 。如果不存在满足要求的 <code>i</code> 和 <code>j</code> ，返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [7,<em><strong>1</strong></em>,<em><strong>5</strong></em>,4]\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>\n",
    "最大差值出现在 i = 1 且 j = 2 时，nums[j] - nums[i] = 5 - 1 = 4 。\n",
    "注意，尽管 i = 1 且 j = 0 时 ，nums[j] - nums[i] = 7 - 1 = 6 &gt; 4 ，但 i &gt; j 不满足题面要求，所以 6 不是有效的答案。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [9,4,3,2]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>\n",
    "不存在同时满足 i &lt; j 和 nums[i] &lt; nums[j] 这两个条件的 i, j 组合。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>nums = [<em><strong>1</strong></em>,5,2,<em><strong>10</strong></em>]\n",
    "<strong>输出：</strong>9\n",
    "<strong>解释：</strong>\n",
    "最大差值出现在 i = 0 且 j = 3 时，nums[j] - nums[i] = 10 - 1 = 9 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == nums.length</code></li>\n",
    "\t<li><code>2 &lt;= n &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [maximum-difference-between-increasing-elements](https://leetcode.cn/problems/maximum-difference-between-increasing-elements/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [maximum-difference-between-increasing-elements](https://leetcode.cn/problems/maximum-difference-between-increasing-elements/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[7,1,5,4]', '[9,4,3,2]', '[1,5,2,10]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        for i in range(len(nums)):\n",
    "            for j in nums[i+1:]:\n",
    "                if nums[i] < j:\n",
    "                    res = max(res, j-nums[i])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        minn, ans = max(nums)+1, 0\n",
    "        for num in nums:\n",
    "            minn, ans = min(minn, num), max(ans, num-minn)\n",
    "        return ans if ans > 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                res = max(res, nums[j] - nums[i])\n",
    "        if res == 0: return -1\n",
    "        else: return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans, premin = -1, nums[0]\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] > premin:\n",
    "                ans = max(ans, nums[i] - premin)\n",
    "            else:\n",
    "                premin = nums[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res=-1\n",
    "        min_nums=nums[0]\n",
    "        for num in nums:\n",
    "            if num<=min_nums:\n",
    "                min_nums=num\n",
    "                continue\n",
    "            elif num-min_nums>res:\n",
    "                res=num-min_nums\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        m, ans = inf, 0\n",
    "        for num in nums:\n",
    "            m, ans = min(num, m), max(ans, num - m)\n",
    "        return ans if ans > 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        ans = 0\n",
    "        min_num = nums[0]\n",
    "        for i in range(n):\n",
    "            if nums[i] < min_num:\n",
    "                min_num = nums[i]\n",
    "            ans_ = nums[i] - min_num\n",
    "            if ans_ > ans:\n",
    "                ans = ans_\n",
    "        if ans == 0:\n",
    "            return -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        i,j = 0,1\n",
    "        l = len(nums)-1\n",
    "        ans = 0\n",
    "        while j<=l:\n",
    "            while j<l and nums[j]>=nums[i]:\n",
    "                ans = max([ans,nums[j]-nums[i]])\n",
    "                j +=1 \n",
    "            ans = max([ans,nums[j]-nums[i]])\n",
    "            i = j\n",
    "            j +=1\n",
    "        return ans if ans>0 else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        def findMax(lst, n):\n",
    "            ma, m = 0, 0\n",
    "            for i in range(len(lst)):\n",
    "                if lst[i] > ma:\n",
    "                    ma = lst[i]\n",
    "                    m = i\n",
    "            return ma, m + n\n",
    "\n",
    "        def findMin(lst):\n",
    "            mi = float(\"INF\")\n",
    "            for i in range(len(lst)):\n",
    "                if lst[i] < mi:\n",
    "                    mi = lst[i]\n",
    "            return mi\n",
    "\n",
    "        ans = -1\n",
    "        j = -1\n",
    "        while j < len(nums):\n",
    "            max_n, max_i = findMax(nums[j + 1:], j + 1)\n",
    "            min_n = findMin(nums[j + 1: max_i])\n",
    "            ans = max(ans, max_n - min_n)\n",
    "            j = max_i\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res=-1\n",
    "        min_num=nums[0]\n",
    "        n=len(nums)\n",
    "        for i in range(n-1):\n",
    "            min_num=min(min_num,nums[i])\n",
    "            diff=nums[i+1]-min_num\n",
    "            if diff>0:\n",
    "                res=max(res,diff)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans,premin = -1,nums[0]\n",
    "        for i in range(1,n):\n",
    "            if nums[i]>premin:\n",
    "                ans = max(ans,nums[i]-premin)\n",
    "            else:\n",
    "                premin = nums[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        maxd=-1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[i]<nums[j]:\n",
    "                    maxd=max(maxd,nums[j]-nums[i])\n",
    "        return maxd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        nnums = [9,4,3,2]\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        min_num = nums[0]\n",
    "        for i in range(n):\n",
    "            if nums[i] < min_num:\n",
    "                min_num = nums[i]\n",
    "            ans = nums[i] - min_num\n",
    "            res.append(ans)\n",
    "        if max(res) == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ma=-1\n",
    "        mi=nums[0]\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i]>mi:\n",
    "                ma=max(ma,nums[i]-mi)\n",
    "            else:mi=nums[i]\n",
    "        return ma"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans=-1\n",
    "        \n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i,len(nums)):\n",
    "                delta=nums[j]-nums[i]\n",
    "                if delta >ans and  delta!=0:\n",
    "                    ans=delta\n",
    "        return ans\n",
    "                    \n",
    "                \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[j] - nums[i] > 0:\n",
    "                    res = max(res, nums[j]-nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res=-1\n",
    "        n=len(nums)\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1,n):\n",
    "                diff=nums[j]-nums[i]\n",
    "                if diff>0:\n",
    "                    res=max(diff,res)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        \n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                tmp = nums[j] - nums[i]\n",
    "                if nums[j] > nums[i] and tmp > res: \n",
    "                    res = tmp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        ans = 0\n",
    "        min_num = nums[0]\n",
    "        for i in range(n):\n",
    "            if nums[i] < min_num:\n",
    "                min_num = nums[i]\n",
    "            ans_ = nums[i] - min_num\n",
    "            if ans_ > ans:\n",
    "                ans = ans_\n",
    "        if ans == 0:\n",
    "            return -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans, premin = -1, nums[0]\n",
    "        for i in range(1, n):\n",
    "            if nums[i] > premin:\n",
    "                ans = max(ans, nums[i]-premin)\n",
    "            else:\n",
    "                premin = nums[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        min_num = nums[0]\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            min_num = min(min_num,nums[i])\n",
    "\n",
    "            diff = nums[i+1] - min_num\n",
    "            if diff > 0:\n",
    "                res = max(res, diff)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = 0\n",
    "        m = inf\n",
    "        for num in nums:\n",
    "            m = min(num,m)\n",
    "            if num - m > res:\n",
    "                res = num - m\n",
    "        return res if res>0 else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        \n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                tmp = nums[j] - nums[i]\n",
    "                if nums[j] > nums[i] and tmp > res: \n",
    "                    res = tmp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if nums[j] > nums[i]:\n",
    "                    ans = max(ans, nums[j] - nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        low = min(nums[0], nums[1])\n",
    "\n",
    "        high = max(-1, nums[1] - nums[0])\n",
    "\n",
    "        for i in range(2, len(nums)):\n",
    "\n",
    "            high = max(high, nums[i] - low)\n",
    "\n",
    "            low = min(low, nums[i])\n",
    "\n",
    "        if high == 0:\n",
    "\n",
    "            return -1\n",
    "\n",
    "        return high\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        for i in range(len(nums)):\n",
    "            a = max(nums[i:])\n",
    "            if a > nums[i]:\n",
    "                ans = max(ans, a - nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        return -1 if (n := len(nums)) and 1 > (r := max(nums[j] - nums[i] for i in range(n) for j in range(i + 1, n))) else r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        m, ans = inf, 0\n",
    "        for num in nums:\n",
    "            m, ans = min(num, m), max(ans, num - m)\n",
    "        return ans if ans > 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        a = -1\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1,n):\n",
    "                if nums[j] - nums[i] > a:\n",
    "                    a = nums[j] - nums[i]\n",
    "        return a if a!=0 else -1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = -1\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if nums[j] > nums[i]:\n",
    "                    res = max(res, nums[j] - nums[i])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        flag = True \n",
    "        max = 0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i,len(nums)):\n",
    "                if nums[i]<nums[j]:\n",
    "                    flag = False\n",
    "                    differ = nums[j]-nums[i]\n",
    "                    if differ>max:\n",
    "                        max = differ\n",
    "        if not flag and max !=0:\n",
    "            return max\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums):\n",
    "        n = len(nums)\n",
    "        max_diff = -1\n",
    "\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1, n):\n",
    "                if nums[i] < nums[j]:\n",
    "                    diff = nums[j] - nums[i]\n",
    "                    max_diff = max(max_diff, diff)\n",
    "\n",
    "        return max_diff\n",
    "\n",
    "# 示例输入\n",
    "nums = [7,1,5,4]\n",
    "\n",
    "# 创建 Solution 类的实例\n",
    "solution = Solution()\n",
    "\n",
    "# 调用方法计算最大差值\n",
    "result = solution.maximumDifference(nums)\n",
    "\n",
    "# 输出结果\n",
    "print(result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        max_diff = -1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[j]- nums[i] > max_diff and nums[j]- nums[i]:\n",
    "                    max_diff =  nums[j]- nums[i]\n",
    "        return max_diff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        l = len(nums)\n",
    "        r = -1\n",
    "        for i in range(l):\n",
    "            for j in range(i+1, l):\n",
    "                if nums[j] - nums[i] > r and nums[j] > nums[i]:\n",
    "                    r = nums[j] - nums[i]\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        INF = float('inf')\n",
    "        minv = INF\n",
    "        ans = -INF\n",
    "        \n",
    "        for num in nums:\n",
    "            if minv < num:\n",
    "                ans = max(ans, num - minv)\n",
    "            minv = min(minv, num)\n",
    "        if ans == -INF:\n",
    "            ans = -1 \n",
    "        return ans \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        for i, x in enumerate(nums):\n",
    "            for y in nums[i + 1:]:\n",
    "                if y > x:\n",
    "                    ans = max(y - x, ans)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        n = len(nums)\n",
    "        for i in range(0,n-1):\n",
    "            for j in range(i,n):\n",
    "                if nums[i] < nums[j]:\n",
    "                    tmp = nums[j] - nums[i]\n",
    "                    if tmp > ans:\n",
    "                        ans = tmp\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        a=c=d=0\n",
    "        b=1000000001\n",
    "        for i in nums:\n",
    "            if i<=a:\n",
    "                d+=1\n",
    "            a=i\n",
    "            b=min(b,i)\n",
    "            c=max(c,a-b)\n",
    "        if d==len(nums)-1:\n",
    "            return -1\n",
    "        else:\n",
    "            return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        result = 0\n",
    "\n",
    "        for i in range(len(nums) - 1):\n",
    "\n",
    "            result = max(result, max(nums[i+1:]) - nums[i])\n",
    "        \n",
    "        if result == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        m, ans = inf, 0\n",
    "        for num in nums:\n",
    "            m, ans = min(num, m), max(ans, num - m)\n",
    "        return ans if ans > 0 else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        if nums==sorted(nums,reverse=True):\n",
    "            return -1\n",
    "        c=0\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[i]>=nums[j]:\n",
    "                    continue\n",
    "                else:\n",
    "                    c=max(c,nums[j]-nums[i])\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        min_ = nums[0]\n",
    "        res = -1\n",
    "        for i in range(1,len(nums)):\n",
    "            if nums[i] > min_:\n",
    "                res = max(res,nums[i]-min_)\n",
    "            else:\n",
    "                min_ = nums[i]\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        for x in range(len(nums) - 1):\n",
    "            for y in range(x + 1, len(nums)):\n",
    "                if nums[y] > nums[x]:\n",
    "                    ans = max(ans, nums[y] - nums[x])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        left = nums[0]\n",
    "        diff = -1\n",
    "        for i in range(1, len(nums)):\n",
    "            if left < nums[i]:\n",
    "                diff = max(diff, (nums[i] - left))\n",
    "            else:\n",
    "                left = nums[i]\n",
    "        return diff\n",
    "\n",
    "    # def maximumDifference(self, nums: List[int]) -> int:\n",
    "    #     n = len(nums)\n",
    "    #     diff = float('-inf')\n",
    "    #     for i in range(n):\n",
    "    #         for j in range(i + 1, n):\n",
    "    #             if nums[i] > nums[j]:\n",
    "    #                 break\n",
    "    #             diff = max(diff, (nums[j] - nums[i]))\n",
    "\n",
    "    #     return diff if diff > 0 else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = -1\n",
    "        pre = nums[0]\n",
    "        for i in range(1,n):\n",
    "            if nums[i] > pre:\n",
    "                diff = nums[i] - pre\n",
    "                if ans < diff:\n",
    "                    ans = diff\n",
    "            else:\n",
    "                pre = nums[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        m, ans = inf, 0\n",
    "        for num in nums:\n",
    "            m, ans = min(num, m), max(ans, num - m)\n",
    "        return ans if ans > 0 else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ret,last=-1,nums[-1]\n",
    "        for i in range(len(nums)-2,-1,-1):\n",
    "            if nums[i]<last:\n",
    "                ret=max(last-nums[i],ret)\n",
    "            last=max(last,nums[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        a = sorted(nums, reverse=True)\n",
    "        if nums == a:\n",
    "            return -1\n",
    "        res = float('-inf')\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i, len(nums)):\n",
    "                res1 = nums[j] - nums[i]\n",
    "                res = max(res, res1)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans,premin=-1,nums[0]\n",
    "        for i in range(1,n):\n",
    "                if nums[i]>premin:\n",
    "                    ans=max(ans,nums[i]-premin)\n",
    "                else:\n",
    "                    premin=nums[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        maxs = -1\n",
    "        for i in range(len(nums) - 1):\n",
    "            # 只有大于0，才行\n",
    "            if max(nums[i+1:]) - nums[i] > 0:\n",
    "                maxs = max(maxs, max(nums[i+1:]) - nums[i])\n",
    "        return maxs\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans,n1 = 0,nums[0]\n",
    "        for n in nums[1:]:\n",
    "            if n > n1:\n",
    "                ans = max(ans,n-n1)\n",
    "            n1 = min(n1,n)\n",
    "        return ans if ans != 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        dmax, vmin = -1, float('inf')\n",
    "        for i in range(n):\n",
    "            if nums[i] > vmin:\n",
    "                dmax = max(dmax, nums[i] - vmin)\n",
    "            else:\n",
    "                vmin = nums[i]\n",
    "        return dmax\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res, max_, min_, tmp = 0, -1, nums[0], 1\n",
    "        while tmp < len(nums):\n",
    "            min_ = min(min_, nums[res])\n",
    "            max_ = max(max_, nums[tmp] - min_)\n",
    "            res += 1\n",
    "            tmp += 1\n",
    "        if max_ == 0:\n",
    "            max_ = -1\n",
    "        return max_\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        \n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                tmp = nums[j] - nums[i]\n",
    "                if nums[j] > nums[i] and tmp > res: \n",
    "                    res = tmp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans, premin = -1, nums[0]\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] > premin: ans = max(ans, nums[i] - premin)\n",
    "            else: premin = nums[i]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        if not nums:\n",
    "            return -1\n",
    "        ans = -1\n",
    "        pre = nums[0]\n",
    "        for i in range(1, len(nums)):\n",
    "            if nums[i] > pre:\n",
    "                ans = max(ans, nums[i] - pre)\n",
    "            \n",
    "            else:\n",
    "                pre = nums[i]\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        s=sorted(nums,reverse=True)\n",
    "        if nums==s:\n",
    "            return -1\n",
    "        else:\n",
    "            n=0\n",
    "            for i in range(len(nums)-1):\n",
    "                for j in range(i+1,len(nums)):\n",
    "                    if nums[j]>nums[i]:\n",
    "                        n=max(n,nums[j]-nums[i])\n",
    "            return n "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans, premin = -1, nums[0]\n",
    "\n",
    "        for i in range(1, n):\n",
    "            if nums[i] > premin:\n",
    "                ans = max(ans, nums[i] - premin)\n",
    "            else:\n",
    "                premin = nums[i]\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "\n",
    "        INF = float('inf')\n",
    "\n",
    "        minv = INF\n",
    "\n",
    "        ans = -INF\n",
    "\n",
    "        for num in nums:\n",
    "\n",
    "            if minv < num:\n",
    "\n",
    "                ans = max(ans, num - minv)\n",
    "\n",
    "            minv = min(minv, num)\n",
    "\n",
    "        if ans == -INF:\n",
    "\n",
    "            ans = -1 \n",
    "\n",
    "        return ans \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res, tmp = -1, nums[0]\n",
    "        for num in nums[1:]:\n",
    "            if num > tmp:\n",
    "                res = max(num-tmp, res)\n",
    "            else:\n",
    "                tmp = num\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans = -1\n",
    "        m = nums[0]\n",
    "        for x in nums[1:]:\n",
    "            if m != x:\n",
    "                ans = max(ans, x - m)\n",
    "            m = min(m, x)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            for j in range(i+1, n):\n",
    "                if nums[j] > nums[i]:\n",
    "                    ans = max(ans, nums[j]-nums[i])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        ans = 0\n",
    "        min_num = nums[0]\n",
    "        for i in range(n):\n",
    "            if nums[i] < min_num:\n",
    "                min_num = nums[i]\n",
    "            ans_ = nums[i] - min_num\n",
    "            if ans_ > ans:\n",
    "                ans = ans_\n",
    "        if ans == 0:\n",
    "            return -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        nnums = [9,4,3,2]\n",
    "        n = len(nums)\n",
    "        res = []\n",
    "        min_num = nums[0]\n",
    "        for i in range(n):\n",
    "            if nums[i] < min_num:\n",
    "                min_num = nums[i]\n",
    "            ans = nums[i] - min_num\n",
    "            res.append(ans)\n",
    "        res = list(set(res))\n",
    "        res.sort()\n",
    "        if res[-1] == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return res[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        maxdiff = -1\n",
    "        for i in range(len(nums) - 1):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if nums[j] - nums[i] > 0:\n",
    "                    maxdiff = max(maxdiff, nums[j] - nums[i])\n",
    "        return maxdiff"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = nums[1] - nums[0]  # 初始化答案为第一个数与第二个数的差值\n",
    "        l = len(nums)  # 获取数组长度\n",
    "        \n",
    "        # 遍历数组，计算相邻两个数之间的差值\n",
    "        for i in range(l - 1):\n",
    "            for j in range(i + 1, l):\n",
    "                ans = max(ans, nums[j] - nums[i])  # 更新答案为所有相邻数之差的最大值\n",
    "                \n",
    "        # 检查答案是否小于等于0\n",
    "        if ans <= 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans, premin = -1, nums[0]\n",
    "\n",
    "        for i in range(1, n):\n",
    "            if nums[i] > premin:\n",
    "                ans = max(ans, nums[i] - premin)\n",
    "            else:\n",
    "                premin = nums[i]\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        res = -1\n",
    "        min_num = nums[0]\n",
    "        n = len(nums)\n",
    "        for i in range(n-1):\n",
    "            min_num = min(min_num,nums[i])\n",
    "\n",
    "            diff = nums[i+1] - min_num\n",
    "            if diff > 0:\n",
    "                res = max(res, diff)\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "  def maximumDifference(self, nums: List[int]) -> int:\n",
    "    ans,n = -1,len(nums)\n",
    "    for i in range(n-1):\n",
    "      for j in range(i+1,n):\n",
    "        ans = max(ans, nums[j]-nums[i])\n",
    "        if not ans: ans =-1\n",
    "    return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        left = 0\n",
    "        ans = 0\n",
    "        for right in range(1, len(nums)):\n",
    "            diff = nums[right] - nums[left]\n",
    "            if diff > ans:\n",
    "                ans = diff\n",
    "            if nums[right] < nums[left]:\n",
    "                left = right\n",
    "        if ans == 0:\n",
    "            return -1\n",
    "        else:\n",
    "            return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        maxs = -1\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i+1,len(nums)):\n",
    "                if nums[j] - nums[i] > 0 :\n",
    "                    maxs = max(maxs,nums[j] - nums[i])  \n",
    "        return maxs\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        cur_min = -1\n",
    "        for i in range(n):\n",
    "            for j in range(i + 1, n):\n",
    "                if nums[j] - nums[i] > 0:\n",
    "                    cur_min = max(nums[j] - nums[i], cur_min)\n",
    "        return cur_min"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ret,last=-1,nums[-1]\n",
    "        for i in range(len(nums)-2,-1,-1):\n",
    "            if nums[i]<last:\n",
    "                ret=max(last-nums[i],ret)\n",
    "            last=max(last,nums[i])\n",
    "        return ret"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ans, premin = -1, nums[0]\n",
    "\n",
    "        for i in range(1, n):\n",
    "            if nums[i] > premin:\n",
    "                ans = max(ans, nums[i] - premin)\n",
    "            else:\n",
    "                premin = nums[i]\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        ans = -1\n",
    "        pre = nums[0]\n",
    "        for num in nums[1:]:\n",
    "            if num > pre:\n",
    "                if num-pre > ans:\n",
    "                    ans = num-pre\n",
    "            else:\n",
    "                pre= num\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        return max(nums[j] - nums[i] for i in range(n) for j in range(i, n)) or -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def maximumDifference(self, nums: List[int]) -> int:\n",
    "        min_num = float('inf')  # 初始化最小值为正无穷大\n",
    "        max_diff = -1  # 初始化最大差值为负数\n",
    "\n",
    "        for num in nums:\n",
    "            if num < min_num:\n",
    "                min_num = num\n",
    "            diff = num - min_num\n",
    "            if diff > max_diff:\n",
    "                max_diff = diff\n",
    "\n",
    "        return max_diff if max_diff > 0 else -1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
